import requests
import json
from threading import Thread
# 添加定时任务所需导入
import time
import schedule

url = "http://218.94.6.94:9091/js-appRole/postEnter"
headers = {
    "authorization":
    "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6ImM3MDdmNWU4MDRmYmQ1NWQ2ZTY5NTRmMjFhYmU2ZjFmIiwicGhvbmUiOiIxMzYyNTI1MjI1MiIsImlkIjoiYzRmMzFiYTExNjY3NDJjNDgwZjc1ZGVhNjIzMWFiOGIiLCJleHAiOjE3NTY0Mjc1OTB9.D-IVx5UWAKeEF6SJHY_ZSbrfDIq3hsMepPshsQbCpfg",
    "Content-Type": "application/json;charset=UTF-8",
    "User-Agent":
    "Mozilla/5.0 (Linux; Android 12; ICL-AL20 Build/HUAWEIICL-AL20; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.196 Mobile Safari/537.36 uni-app Html5Plus/1.0",
    "Accept": "application/json, text/javascript, */*;q=0.01",
    "X-Requested-With": "com.jssq.android",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh,en-US;q=0.9,en;q=0.8",
}
js_login = {
    "funcName":
    "rsMMI6evsC598vyCjuyE6ckT0uMOuDaaKkNrKdjtSpCwyLYx6GqyDgDFLT7Ir7QaU0gcmwE16uaHxmeCk9Ftq/FpYnVe5arGzqQ5UafjbJow7MK5tW1OPUCHgIpO7PgiPl//GkNlAXhyBxxIwnpymKLIuCexC3JuivQ41Tchps0=",
    "phone":
    "wqwcSTnj7h9hNEWKi6oY1xw3i1wzQj3Kms1LaaDkRN2//ix7scn9W4voToBR3/qn6B/qX7jLRzb5Pwxmeiz6ibTx6BaMUmBjo+hZ9j59ENctm73i93JnL8DTwxLTpzl5eZT9fHxCLyJR7Mzvr8ScAqSVhJOAB9na3rhuEppED2Q=",
    "pass":
    "UrtQVi2pB8q/LE+zYEKHsf4WkaOcDOApNe3YBgAj519NOlXfrxBfFSoC/xejb9OChwd7tgb7orD1aBJl0++Rh+ijHenU1CJvckJz/Wg0utrcwRRwOyQgh5Zr7EL1JevryyMKsaQ0EtTXbjNzuj67R8HCFhMyD2qQ5yatUMvQgIc=",
    "uuid":
    "L+A1dPT+dF/eCUbjCUtLWL+/Hw6xhLHHnwu8VIsfbnrqdsF7Iqf/VjUwSZud3FjzBAt322bcPF3LG1gQlDO9n18EP+zLml7A437T76pJBPezL8kh35r8yDnPQNw+VMDXcDuwiIvq1nuqyXWz/6/2bFdhUgni+BPZ8k3FfwjZqCo=",
    "encrypt":
    "XZuOO84SPnrnOGJOXXUCSyZoc2b+0C9mtkeiFCYWp0AkPUgCq0P3DldyGhFX986B66UldZkdXA7k+f8lf9+3PpWetv22fCYPgS37hzRLo6jH5+zZ+m8Kd99bOBuCn2g3+h3PT3e0mU3Bexb0KZWs8H7yP+Joqnq4SiIJaBPd3NE="
}
js_list = [
    # 水位、流量
    {
        "funcName":
        "A5w5MDUKySRYVAj6sDE7063hqqhLozujqbnVnQXFq3gFxo4uPW5ZLnmIYqYUqFJTTLEvAC1vom17LizytpB9wLfpU7jxMWip1POVp27v14S7zwVnh/dUtBZzTJu1LA6yrb/RY3EI3Z66Wlc8MkzN6xeC0cTP8A+wCOJQkam9fuE=",
        "index":
        "dspLwC3UFjF1E4bYtrlti1Me2Jz5xGmSbR879PyfxUTdClv2W1f0qPybaqJxCe5bcR0gI46SBMOF4hhcSbmrum1dBYOKAHh2JsZ1FOFjqBt0OspsDVaredx1kYIuJmyJM0NYdMEcz5Fi3SrlNXKKwGkNDVUtRQw5mXS31PnhuPQ=",
        "length":
        "ZDMPtUGZvd9R0fnJHjC9T98ZJHHV8IokJUenlrUckm4RinW4C9gz0VxkAu43r4pZNfjTBJjwo8cmt1/d3kstrow+U/lmMCvok0BhVZczsPraNf3UeLLNnlQP50LOg0OuXuAFJPU2YfOhXaT5WsAi8l0iLiihERybjNS0etViuMo=",
        "type":
        "u8lTgqfoSxbh2C0T+W4Qd006qtoVPvdqLlFTrNzv5ZPI9CaiBgjazbS5LpDqDe3iKkSbTgBnfnbfhmAYYFSEiMvW/dQbTGyFUdJ3tptHDUTehV4rYs+q/eo7rhclujhPIvtyKuI18M2PhBvXWkC+fI9484iBZeS1ScHUN+++Zos=",
        "stnm":
        "CiwfiO2/Y+QPXiXJxEzOurHjQ5j2sPqa7/EqKeSKyG4bXsfNdnz1Ow6xaOxtBmS1omUJIv3IJcWkXH9SkBbxrTcuCLfXmudlHShCxZxL3Fx28P+JHELyaiezPozQJE60Evp6n3sagHs1zC+W7UD86kYJBOQynZ/vSZ+6X8gK26s=",
        "hnnm":
        "AwqpUyf517d+0xYo8XC1wCrpb2GBEZX51vkWRj/TaqmhQ9si6Obqua5tyrdc4k1M49t1oAKoVzUVNCSL/PcV8ERO5U0qetMw6gQ62bSd+lKwodBx0JvBw0yYg9G+0icM7lb9Ko+EcQqfcLUVBLagXjJZm5qJgiyxOW3TROgbyzA=",
        "addvcd":
        "JbClcQCnb7ajRGFNuVcRY+9WNuidUjh05EQ+QFgUGRWD0Vck5ML/WH70YqmUSfontcLoW7PGuz1rQFaOSiXNwh9q6PTzpbMnCgTJW2MUqayvoP26jvVbq8HGKw6YnEeyRguEy7dO6I4ZSE330rqBndL2mbRDPOFR1hGkUBVcKEA=",
        "5m":
        "YdtIy9RHW+ws+mpbGsyL7CA33gXHraRW+k92Da0hMp0On+BsI8d3pzhTfnAjBT0PlEKa9NC31+nLVeMi+b1ltjwtsuHrHjd/P3gsG/9A78fIddzPCnq7Snr0vg7slS9q0vz7fw7YhuJm2s1yDq/Yxmd2sJHelVeb74LGrrE3XcA=",
        "phone":
        "A5bmHvu1pS1SsmUx4j5IIbN+OfMIIgESDCP9/4Vc19A9cke6mVdz3HkQt0sSRfgvNBe1DlWZubU4gdHm3UUVNciCYof82mIh3IAjSg/RnVJhrzRJn4w0rcBU/Zn6fHXiwBu8z2YOIEi2Rns9Ya4gf6ayYUS3fx40SBYKRqO8Tvk="
    },
    # 降雨
    {
        "funcName":
        "PWvfeNwCgTA4WICMM5CQezToCveZKtZmFj4FVkbGqbXjS3EScFzW1+r/mdX9yguaOMwmknKZSRpKDf9MD8g/IwhO4ZZwwXcB/acpH/DPexCs6zHbUcFS7Xqm3MJvXe3JSYc2fzgvltXyaXgHsgioBwicHA9VdQteM/LBnFDBExQ=",
        "5m":
        "w5RN70RnH8dLvUADWQImFX3Yhb30hoPlTA5sGGnW0bYkwEhuod2wL94cVh5Ba78UcYi3LisQGsst1O+E65Q5cVSPSsq2Nf3CX0fqcRIpn5o2m54UsyAZCpzOYCA48Ik022eodQvCD4Ww9LlILI6vScNDklFyHtJtgdDPK9qXcuQ=",
        "index":
        "uS8Q+Q1XY0xMmPRhlMpqvn69JERndQicaSp8YYtfbU/jNAgXSSTMHtnVLY2xAm1hvW8Rz3c0afKkMJGmUiaZOzAbNaj0+2keaPFoi7WF6Bm4bOkjni6+qPldLI+gOF8HOsyZZM2i+PJONuyHEZsk3swVUX/723ore4ltCV19Bac=",
        "length":
        "UMdj52zPVPIjzDyfiRu85Ec/iPwHNia/aciVYuD4Ndx+e585pREVVcQ3uLqgINqLBQ6v+NYXE+J19728e4Wwnh0kktA5oAzKoQ5/1uN7tCmVuuMQIk1ztwjJ9bI8yFrZcbYN21K72SGgJhy6i2okRAZLXIZearWo1wRoM8TOUac=",
        "phone":
        "ornhvB3qZ3FGlUijzB162kDnb9U87YpjJ9LH8Upq9HRLrQgkgjAirDg1Zh6NO6f0jecfnz4GimqIogeup7DE3ErAwhB4U+X4MgJtHObr4zL5f4UrDwUQz5QP9b++NqLtDpvFQrr4Sh32Gwc+QXMrUGXRFUjZLwJm4u8z1osYo7s="
    },
    # 入湖
    {
        "funcName":
        "PXlbYGS8h1Z28sbPQ+B8utxsDMDcYbZURhNthH+Dd/pkEKcA5IMF9EVviSxNmnEGP+r8Gn2tH58b86OoRb/Wq1Pzd5zpdGmXh5C71G8PtD5Aitt7UbWDm8AgJ8erBfTxkpF/J+HGSd1CuzjH+Zaz114/DY2Ux7I/uGmrxUjf9vI\u003d",
        "day":
        "jfIZfwh0d5nFrQapEKX9lOBOdGQJmUkf36OJgFUQSOHG5UwEkepJwdMhMYarKmnsHlESJT0DGWBCXNIsbPFyAwszZatZHZk5n/fO4HBWHi4sbrvmei7p8830gDzDH16UzsQcCA1F1e8+ecTHheSR23F+p/t2c6QfWtSxCDf7Ak0=",
        "phone":
        "PrnlCSqYe/PinhBEMRGDdmk8h/zPMRLIw5UuJTUZeHGpG9cjbneQ+VLwAk+0zkrHrgNb37+8/iMtNR9j14CRowSi/yB1cxgwBZOBgVY6n4cimkEiIDQZf5A6/k7Horv591+/fbXgztzboZ7b1xBceSq+ExdyPaYBA5nQSJJPmnM\u003d"
    }
]


def getAppDataByurl(url, headers, js, jsfilename):
    resp = requests.post(url=url, headers=headers, json=js)
    if resp.status_code == 200:
        print(len(resp.text))
        with open(f"./data/{jsfilename}.json", "w", encoding='utf8') as f:
            f.write(json.dumps(resp.json(), indent=4, ensure_ascii=False))
            print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                  f"数据已保存到 ./data/{jsfilename}.json")
        return resp.json()


def getAppData():
    th_list = []
    # jsfile_list =
    for index, js in enumerate(js_list):
        th_list.append(
            Thread(target=getAppDataByurl,
                   args=(url, headers, js, f"jssqapp{index}")))
    for th in th_list:
        th.start()
    for th in th_list:
        th.join()

    # return getAppDataByurl(url, headers, js)


# 添加定时执行函数
def run_schedule():
    # 修改为每小时的30分执行一次
    schedule.every().hour.at(":10").do(getAppData)

    while True:
        schedule.run_pending()
        time.sleep(60)


def lmh(url):
    # url = 'http://218.94.6.94:9091/js-appOS/postEnter'
    js = {
        "funcName": "getWaterDisplay",
        "stcd": "51107800",
        "type": "lake",
        "min5": 0,
        "btime": "",
        "etime": ""
    }
    resp = requests.post(url=url, headers=headers, json=js)
    return resp.text


def zsz(url):
    # url = 'http://218.94.6.94:9091/js-appOS/postEnter'
    js = {
        "funcName": "getWaterDisplay",
        "stcd": "51102701",
        "type": "river",
        "min5": 0,
        "btime": "",
        "etime": ""
    }
    resp = requests.post(url=url, headers=headers, json=js)
    print(resp.text)


if __name__ == "__main__":
    # print(zsz(url="http://218.94.6.94:9091/js-appOS/postEnter"))
    # 修改主函数以支持定时执行
    # print("开始定时任务，每小时的10分执行一次getAppData()")
    # run_schedule()
    getAppData()
    # getAppDataByurl(url, headers, js_login, "js_login")
